﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BacklogParser.Parsers;

namespace BacklogParser.Models
{
    public class RelatedDocument
    {
        public RelatedDocument()
        {
            this.Relation = new Relation();
        }


        public string Continuation { get; internal set; }
        public string PreviouslyFiledApplication { get; internal set; }
        public Relation Relation { get; internal set; }

        public override string ToString()
        {
            var builder = new StringBuilder();

            if (!string.IsNullOrEmpty(Continuation))
                builder.AppendLine(string.Format("    Continuation: {0}", Continuation));

            if (!string.IsNullOrEmpty(PreviouslyFiledApplication))
                builder.AppendLine(string.Format("    PreviouslyFiledApplication: {0}", PreviouslyFiledApplication));

            if (!string.IsNullOrEmpty(Relation.ToString()))
                builder.AppendLine(string.Format("    Relation: {0}", Relation.ToString()));

            if(string.IsNullOrWhiteSpace(builder.ToString().Trim()))
            {
                return "";
            }
            else
            {
                return "        Related Document\n" + builder.ToString();
            }

        }

        public string GetDate()
        {
            return this.GetChildData() + this.GetParentData();
        }

        public string GetChildData()
        {
            var builder = new StringBuilder();

            builder.Append(this.Continuation);
            builder.Append(this.PreviouslyFiledApplication);

            builder.Append(this.Relation.ChildDoc.ChildGrantDocument);
            builder.Append(this.Relation.ChildDoc.ChildPctDocument);

            builder.Append(this.Relation.ChildDoc.ChildStatus);
            builder.Append(this.Relation.ChildDoc.DocumentIds.Country);
            builder.Append(this.Relation.ChildDoc.DocumentIds.Date);
            builder.Append(this.Relation.ChildDoc.DocumentIds.DocNumber);
            builder.Append(this.Relation.ChildDoc.DocumentIds.Kind);
            builder.Append(this.Relation.ChildDoc.DocumentIds.Lang);
            
            return builder.ToString();
        }

        public string GetParentData()
        {
            var builder = new StringBuilder();

            builder.Append(this.Continuation);
            builder.Append(this.PreviouslyFiledApplication);
            builder.Append(this.Relation.ParentDoc.ParentGrantDocument);
            builder.Append(this.Relation.ParentDoc.ParentPctDocument);
            builder.Append(this.Relation.ParentDoc.ParentStatus);
            builder.Append(this.Relation.ParentDoc.DocumentIds.Country);
            builder.Append(this.Relation.ParentDoc.DocumentIds.Date);
            builder.Append(this.Relation.ParentDoc.DocumentIds.DocNumber);
            builder.Append(this.Relation.ParentDoc.DocumentIds.Kind);
            builder.Append(this.Relation.ParentDoc.DocumentIds.Lang);

            return builder.ToString();
        }
    }
}
